package com.wits.qso.dao.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.CourseMineCustomDao;
import com.wits.qso.entity.Course;
import com.wits.qso.entity.CourseMine;
import com.wits.qso.pageable.NativeSqlPageable;

public class CourseMineDaoImpl extends BaseDaoImpl<Course, String> implements
		CourseMineCustomDao {
	
	@Override
	public Page<CourseMine> findCourseMineViewPages(Map<String, Object> param) {
		PageImpl<CourseMine> pipl=null;
		if (param == null) {
			 return pipl;
		}
		StringBuffer buf = new StringBuffer();
		buf.append("SELECT DISTINCT a.course_id,a.date_cre,a.date_upd, a.remark, a.user_cre," +
				"a.user_upd,a.course_code,a.course_date_f,a.course_date_t,a.course_period," +
				"a.course_title FROM qso_course a  WHERE 1=1 ");
		if(param.get("courseIds")!=null){
			buf.append(" AND a.course_id IN(");
			Object[] ids=(Object[]) param.get("courseIds");
			for(int i=0;i<ids.length;i++){
				buf.append("'"+ids[i].toString()+"'");
				if(i<ids.length-1){
					buf.append(",");
				}
			}
			buf.append(") ");
		}
		
		if(param.get("title")!=null){
			buf.append(" AND a.course_title LIKE '%"+param.get("title")+"%'");
		}
		
		//查询总数
		StringBuffer sqlBuf00=new StringBuffer("");
		sqlBuf00.append(buf);
		sqlBuf00.append(" ");
		//分页查询起始行，每页行数
		StringBuffer sqlBuf01=new StringBuffer();
		sqlBuf01.append(buf);
		String[] orderByArray=(String[])param.get("orderByArray");
		if(orderByArray!=null&&orderByArray.length>0){
			sqlBuf01.append(" order by ");
			for(int i=0;i<orderByArray.length;i++){
				String str=orderByArray[i];
				if(str!=null){
					if(i>0){
						sqlBuf01.append(",");
					}
					sqlBuf01.append(str);
				}
			}
		}
		sqlBuf01.append(" limit :startLine,:lineEveryPage ");
		Query query00 = em.createNativeQuery(sqlBuf00.toString(),Course.class);
		Query query = em.createNativeQuery(sqlBuf01.toString(), Course.class);
		NativeSqlPageable ctp=getNativeSqlPageable(param);
		int pageNum=ctp.getPageNumber();
		int pageSize=ctp.getPageSize();
		query.setParameter("startLine", (pageNum-1)*pageSize);
		query.setParameter("lineEveryPage", pageSize);
		//查询总数
		int total=query00.getResultList().size();
		//查询记录
		@SuppressWarnings("rawtypes")
		List list=query.getResultList();
		
		List<CourseMine> ls=new ArrayList<CourseMine>();
		CourseMine cm=null;
		Map<String, Object> teas=(Map<String, Object>) param.get("teas");
		for (Object o : list) {
			Course course=(Course) o;
			cm=new CourseMine();
			cm.setId(course.getId());
			cm.setTitle(course.getTitle());
			cm.setTeaName(""==teas.get(course.getId()).toString()?"":teas.get(course.getId()).toString());
			ls.add(cm);
		}
		
		List<CourseMine> destList=new ArrayList<CourseMine>(); 
		if(param.get("teacher")!=null){
			for (CourseMine courseMine : ls) {
				if(courseMine.getTeaName().contains(param.get("teacher").toString())){
					destList.add(courseMine);
				}
			}
		}else{
			destList=ls;
		}

		pipl=new PageImpl<CourseMine>(destList, ctp, total);
		
		return pipl;
	}
	
	private NativeSqlPageable getNativeSqlPageable(Map<String,Object> param){
		NativeSqlPageable ctp=new NativeSqlPageable();
		Integer pageNumber=param.get("pageNumber")==null?new Integer(1):(Integer)param.get("pageNumber");
		Integer pageSize=param.get("pageSize")==null?new Integer(20):(Integer)param.get("pageSize");
		ctp.setPageNumber(pageNumber.intValue());
		ctp.setPageSize(pageSize.intValue());
		return ctp;
	}

}
